#!/usr/bin/env python

import os
import logging

from tornado.options import define, options, parse_command_line
from tornado.ioloop import IOLoop

from handler.login_handler import LoginHandler, EchoService
from handler.index_handler import IndexHandler
from settings import COOKIE_SECRET

import pyrestful.rest

define("port", default=9999, type=int)

def create_server():

	logging.debug("parse command line...")
	parse_command_line()
	handler_list = [
		LoginHandler, EchoService, IndexHandler
	]
	logging.debug("create app by service list...")
	app = pyrestful.rest.RestService(handler_list, cookie_secret=COOKIE_SECRET)
	logging.debug("listening in " + str(options.port))
	app.listen(options.port)
	logging.debug("instance start ...")
	IOLoop.instance().start()

if __name__ == "__main__":

	try:
		create_server()
	except KeyboardInterrupt, e:
		print "Canceled by user typed : Ctrl+C ", e
	except Exception, e:
		print e

